<?php
include_once ('lib/Pagination.php');require ('Zend/Cache.php');require_once ('Zend/Filter.php');
class IndexController extends Zend_Controller_Action {
	private $cache;
	private $db;
	/**
	 * 初始化
	 * @access public
	 */
	function init() {
		$this->db = Zend_Db_Table::getDefaultAdapter ();
		$frontendOptions = array ('lifeTime' => CACHE_LIFE_TIME, // cache lifetime of 2 hours
									'automatic_serialization' => true );
		$backendOptions = array ('cache_dir' => CACHE_TMP_DIR );// Directory where to put the cache files
		$this->cache = Zend_Cache::factory ( 'Core', 'File', $frontendOptions, $backendOptions );
		$this->view->title = WEBTITLE;
		if (! $disRes = $this->cache->load ( 'disRes' )) {
			$this->view->disResSection = $this->getDisCommend ();
			$this->cache->save ( $this->view->disResSection, 'disRes' );
		} else
			$this->view->disResSection = $disRes;
		$type = $this->getSearchType();
		$this->view->bTypeSection = $type['bType'];
		$this->view->sTypeSection = $type['sType'];
		//总数
		$this->getCompanyCnt();
		$this->view->disCnt = $_SESSION['comcnt'];
		$this->view->addHelperPath(VIEW_HELPER_PATH, 'My_View_Helper');
	}
	/**
	 * 取公司总数
	 * @access private
	 * @return void
	 */
	private function getCompanyCnt(){
		$company = new Company();
		$comCnt = $company->getCompanyCnt();
		$_SESSION['comcnt'] = $comCnt;
	}
	public function indexAction() {
		$rowsPerPage = 20;
		$currPage = $this->_request->getParam ( 'page', 1 );
		$offset = ($currPage - 1) * $rowsPerPage;
		$comCnt = $_SESSION['comcnt'];
		if (($comCnt - $offset) > $rowsPerPage) {
			$count = $rowsPerPage;
		} else {
			$count = $comCnt - $offset;
		}
		$select = $this->db->select ();
		$select->from ( 'cc_company' )->where ( 'state=1' )->order ( 'power desc' )->limit ( $count, $offset );
		$selectSql = $select->__toString ();
		$companyTag = md5 ( $selectSql );
		if (! $comPageRes = $this->cache->load ( $companyTag )) {
			$res = $this->db->fetchAll ( $select );
			$this->cache->save ( $res, $companyTag );
		} else {
			$res = $comPageRes;
		}
		
		$this->view->comResSection = $res;
		include_once("lib/Pagination.php");
		$Pager = new Pagination ( $comCnt, $rowsPerPage );
		$this->view->pagebar = $Pager->getNavigation ();
	}
	
	/**
	 * 取优惠推荐信息
	 * @return array优惠推荐信息数组
	 */
	private function getDisCommend() {
		$discommendObj = new Discommend ( );
		$disRes = $discommendObj->getTop ( 10 );
		return $disRes;
	}
	/**
	 * 取搜索类型：包括大类型、小类型
	 * @return array 搜索类型
	 */
	private function getSearchType(){
		$search = new Seek();
		$type['bType'] = $search->getbType();
		$type['sType'] = $search->getsType();
		return $type;
	}
	
	public function companyAction() {
		$companyid = Zend_Filter::get ( $this->_request->getParam ( 'id', 0 ), "Digits" );
		$discountid = $this->_request->getParam ( 'disid', 0 );
		$this->view->disCountId = $discountid;
		if ($companyid == 0) {
			$this->_redirect ( "../" );
		} else {
			$company = new Company ( );
			$comSet = $company->find ( $companyid );
			if ($comSet->count () > 0) {
				$comSetArr = $comSet->toArray ();
				$companyClickCnt = empty ( $comSetArr [0] ['clickcnt'] ) ? 0 : $comSetArr [0] ['clickcnt'];
				$company->addClick ( $companyid, $companyClickCnt + 1 );
				$this->view->conameSection = $comSetArr [0] ['name'];
				
				//添加点击记录
				$click = new Click();
				$data = array('cdate'=>date('Ymd h:i:s'),'cip'=>$_SERVER['REMOTE_ADDR'],'ctype'=>'C','cid'=>$companyid);
				$click->addClickLog($data);
				
				$discount = new Discount ( );
				$select = $discount->getAdapter ()->select ();
				$select->from ( 'cc_discount' )->where ( 'coid=?', $companyid )->where ( 'state=1' )->order ( 'clickcnt desc' );
				$selectSql = $select->__toString ();
				$companyTag = md5 ( $selectSql );
				if (! $selectRes = $this->cache->load ( $companyTag )) {
					$res = $this->db->fetchAll ( $select );
					$this->cache->save ( $res, $companyTag );
				} else {
					$res = $selectRes;
				}
				
				if (count ( $res ) > 0) {
					$this->view->discountSection = $res;
				} else {
					$this->view->msgSection = "此公司无折扣信息";
				}
			} else {
				$this->view->discountSection = array ();
				$this->view->msgSection = "无此公司信息";
			}
		}
	}
	
	public function printdisAction() {
		$disId = $this->_getParam ( 'id', 0 );
		if ($disId == 0) {
			$this->_redirect ( "../" );
		} else {
			$discount = new Discount ( );
			$disSet = $discount->find ( $disId );
			if ($disSet->count () > 0) {
				//添加点击记录
				$click = new Click();
				$data = array('cdate'=>date('Ymd h:i:s'),'cip'=>$_SERVER['REMOTE_ADDR'],'ctype'=>'D','cid'=>$disId);
				$click->addClickLog($data);
				
				$disSetArr = $disSet->toArray ();
				$this->view->disRes = $disSetArr;
			} else {
				$this->view->disRes = array ();
				$this->view->msgSection = "无此优惠信息!";
			}
		}
	}
	
	public function searchAction() {
		$bSelType = $this->_getParam('bseltype','all');
		if ($bSelType == "all") 
			$selectType = "%";
		else 
			$selectType = $bSelType;
		$this->view->selectTypeSection = $bSelType;
//		$sSelType = $this->_getParam('sseltype','all');&&($sSelType=='all')
		$company = new Company();
		
		$rowsPerPage = 20;
		$currPage = $this->_request->getParam ( 'page', 1 );
		$offset = ($currPage - 1) * $rowsPerPage;
		$comCnt = $company->getCompanyByCond($bSelType);
		if (($comCnt - $offset) > $rowsPerPage) {
			$count = $rowsPerPage;
		} else {
			$count = $comCnt - $offset;
		}

		$select = $this->db->select ();
		$select->from ( 'cc_company' )->where ( 'state=1 and btype like ?',$selectType )->order ( 'power desc' )->limit ( $count, $offset );
		$selectSql = $select->__toString ();

		$companyTag = md5 ( $selectSql );
		if (! $comPageRes = $this->cache->load ( $companyTag )) {
			$res = $this->db->fetchAll ( $select );
			$this->cache->save ( $res, $companyTag );
		} else
			$res = $comPageRes;
		
		$this->view->comResSection = $res;
		$Pager = new Pagination ( $comCnt, $rowsPerPage );
		$this->view->pagebar = $Pager->getNavigation ();
	}
	
	
	
	//暂时不用
	public function discountAction() {
		$disId = $this->_getParam ( 'id', 0 );
		if ($disId == 0) {
			$this->_redirect ( "../" );
		} else {
			$discount = new Discount ( );
			$disSet = $discount->find ( $disId );
			if ($disSet->count () > 0) {
				$disSetArr = $disSet->toArray ();
				$clickcnt = empty ( $disSetArr [0] ['clickcnt'] ) ? 0 : $disSetArr [0] ['clickcnt'];
				$discount->addClick ( $disId, $clickcnt + 1 );
				
				$this->view->disRes = $disSet->toArray ();
			} else {
				$this->view->disRes = array ();
				$this->view->msgSection = "无此优惠信息!";
			}
		}
	}
	
	public function infoAction() {
		phpinfo ();
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function clearAction() {
		$this->_helper->viewRenderer->setNoRender ();
		$this->cache->clean ( Zend_Cache::CLEANING_MODE_ALL );
	}
	
	public function __call($name, $arg) {
		$this->_redirect ( "../" );
	}

}